package springapp.web;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import springapp.entity.CompanyEntity;
import springapp.entity.DesignationEntity;
import springapp.entity.ReviewEntity;
import springapp.service.CompanyService;
import springapp.service.IDesignationService;
import springapp.service.ILocationService;
import springapp.service.ReviewService;
import springapp.util.Utils;

@Controller
public class AddReviewController implements IControllerConstants {

	@Autowired(required = true)
	private CompanyService companyService;

	@Autowired(required = true)
	private ReviewService reviewService;

	@Autowired(required = true)
	private IDesignationService designationService;

	@Autowired(required = true)
	private ILocationService locationService;

	@RequestMapping(value = "/AddReviewD.htm", method = RequestMethod.GET)
	public ModelAndView handleRequest(@RequestParam("review") String review,
			@RequestParam("rating") String rating,
			@RequestParam("companyId") String companyId,
			@RequestParam("gender") String gender,
			@RequestParam("exp") String experience,
			@RequestParam("desig") String designation,
			@RequestParam("jobType") String jobType,
			@RequestParam("location") String location) throws Exception {

		DesignationEntity entity = designationService
				.getDesignationEntity(designation);
		Integer locationId = locationService.getLocationId(location);
		int companyId2 = Utils.getInt(companyId);
		ReviewEntity reviewEntity = reviewService.addReview(companyId2, review,
				Utils.getInt(rating), gender, Utils.getInt(experience),
				locationId, entity.getId(), jobType);

		CompanyEntity companyEntity = this.companyService.addReview(companyId2,
				reviewEntity, entity);
		this.companyService.setIndustryName(companyEntity);
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put("company", companyEntity);
		
		return new ModelAndView("Company.jsp", "jsonMap", jsonMap);
	}

}
